package andev.aandroidlib.listener;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public abstract class ATouch implements OnTouchListener {
    private boolean MOVE_MODE = true;
    private float downX, downY;
    private float distanceLast;

    private float getPointerDistance(MotionEvent event) {
        float a = event.getX(0) - event.getX(1), b = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(a * a + b * b);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction() & event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                MOVE_MODE = true;
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                MOVE_MODE = false;
                distanceLast = getPointerDistance(event);
                break;
            case MotionEvent.ACTION_MOVE:
                if (MOVE_MODE) {
                    float nowX = event.getX(), nowY = event.getY();
                    move(v, nowX - downX, nowY - downY);
                    downX = nowX;
                    downY = nowY;
                } else {
                    if (event.getPointerCount() < 2) break;

                    float distanceNow = getPointerDistance(event);
                    zoom(v, distanceNow / distanceLast);
                    distanceLast = distanceNow;
                }
                break;
            case MotionEvent.ACTION_UP:
                v.performClick();
                break;
        }
        return true;
    }

    ;

    /**
     * 手指在屏幕上移动
     *
     * @param v
     * @param xChange 与前一次move点的x距离
     * @param yChange 与前一次move点的y距离
     */
    public void move(View v, float xChange, float yChange) {
    }

    /**
     * 两指对屏幕伸缩
     *
     * @param v
     * @param scale 与前一次相对的伸缩比
     */
    public void zoom(View v, float scale) {
    }

    ;
}
